<template>
	<view class="w-100">
		<view class="flex-1" style="background-color: #000;color: #fff;">
			<view class="flex-1 mx-3 py-3">
				<text class="font-s-4" style="line-height: 2.2;">成为代理可享受8折优惠，分享给其他用户可赚取当中价差，更有机会成为城市合伙人身份享受更低优惠。</text>
			</view>
		</view>
		<view class="flex-1 mt-3 mx-3">
			<view class="font-s-45 py-2">当前身份：{{ useridentity.identity_name }}</view>
			<view class="flex-1 row-ac mt-3 border p-3"
				v-if="useridentity.identity !== 5 && useridentity.identity !== 7">
				<view class="font-s-4 flex-1">我的推荐人: {{ detail.leader_nickname }}</view>
				<view class="phone-box font-s-2" @click="takePhone(detail.leader_phone)">联系他/她</view>
			</view>
			<view class="flex-1 row-ac mt-3 border p-3"
				v-if="useridentity.identity !== 5 && useridentity.identity !== 7">
				<view class="font-s-4 flex-1">所属分公司: {{ detail.superiors_info.nickname }}</view>
				<view class="phone-box font-s-2" @click="takePhone(detail.superiors_info.phone)">联系分公司</view>
			</view>
			<view class="flex-1 row-ac mt-3 border p-3" v-if="useridentity.identity == 5">
				<view class="font-s-4 flex-1">所属股东: {{ detail.shareholder_info.nickname }}</view>
				<view class="phone-box font-s-2" @click="takePhone(detail.shareholder_info.phone)">联系股东</view>
			</view>
			<view class="flex-1 row-ac mt-3 border p-3">
				<view class="font-s-4 flex-1">ELIANTO官方</view>
				<view class="phone-box font-s-2" @click="takePhone('13381173888')">联系平台</view>
			</view>
		</view>
		<view class="flex-1 mt-9 row" v-if="useridentity.identity == 1" @click="takeAppply">
			<view class="apply-box row" v-if="useridentity.addly_status == null">申请代理</view>
			<view class="apply-box row" v-if="useridentity.addly_status == 2">审核中....</view>
		</view>
	</view>
</template>

<script>
	import {
		useridentityDetailUrl,
		distributorsDetailUrl,
		useridentityapplyPatchUrl
	} from "@/api/index.js"
	export default {
		data() {
			return {
				detail: '',
				useridentity: '',
				formData: {
					addly_identity: 2,
					addly_status: 2
				}
			}
		},
		onLoad() {
			this.init()
		},
		methods: {
			init() {
				this.getDistributorsDetail()
				this.getUseridentityDetail()
			},
			getUseridentityDetail() {
				this.$R.get(useridentityDetailUrl).then(res => {
					this.useridentity = res.data
				})
			},
			getDistributorsDetail() {
				this.$R.get(distributorsDetailUrl).then(res => {
					this.detail = res.data
				})
			},
			takeAppply(){
				if(this.useridentity.addly_status == null){
					uni.showModal({
						title: '申请代理',
						content: '是否申请成为代理？',
						cancelText: '取消',
						confirmText: '确认申请',
						success: (res) => { // 使用箭头函数
							if (res.confirm) {
								this.$R.patch(useridentityapplyPatchUrl + this.useridentity.id + '/',this.formData).then(res=>{
									if(res.code == 200){
										uni.showToast({
											title: '申请成功',
											icon: 'none'
										});
										this.getUseridentityDetail()
									}
								})
							} else if (res.cancel) {
								uni.showToast({
									title: '取消申请',
									icon: 'none'
								});
							}
						}
					});
					
				}else{
					uni.showToast({
						title: '您已申请，正在审核中......',
						icon: 'none'
					});
				}
			},
			takePhone(number) {
				console.log(number);
				uni.makePhoneCall({
					phoneNumber: number
				});
			}
		}
	}
</script>

<style>
	.phone-box {
		padding: 13rpx 18rpx;
		background-color: #000;
		color: #fff;
	}

	.apply-box {
		background-color: #000;
		color: #fff;
		width: 320rpx;
		height: 96rpx;
	}
</style>